<template>
    <div>
        <h2>添加用户</h2>
        <table border="1">
            <tr>
                <td>用户名</td>
                <td>
                    <input type="text" placeholder="请输入用户名" v-model="info.Name">
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td>
                    <input type="password" placeholder="请输入密码"v-model="info.Pwd">
                </td>
            </tr>
            <tr>
                <td>手机号</td>
                <td>
                    <input type="text" placeholder="请输入手机号" v-model="info.Phone">
                </td>
            </tr>
            <tr>
                <td>头像</td>
                <td>
                    <img :src="info.Pic" style="max-width: 70px; max-height: 70px;">
                    <input type="file" @change="load">
                </td>
            </tr>
            <tr>
                <td>邮箱</td>
                <td>
                    <input type="text" placeholder="请输入邮箱" v-model="info.Email">
                </td>
            </tr>
            <tr>
                <td colspan="">
                    <input type="button" value="添加" @click="add">
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted}from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
let router=useRouter();
const info=ref({
    Name:'',
    Pwd:'',
    Phone:'',
    Pic:'',
    Email:'',
    State:true
})

const load=(e:any)=>{
let formData=new FormData();
let file=e.target.files[0];
formData.append('file',file);
let imgAll=["image/jpeg","image/png","image/gif"];
if(imgAll.indexOf(file.type)==-1){
    alert("上传文件必须是图片");
    return;
}
if(file.size>2*1024*1024){
    alert("文件大小不能超过2M");
    return;
}
axios.post('https://localhost:7294/api/Member/LoadPic',formData)
.then(res=>{
    info.value.Pic=res.data;
})
.catch(err=>{
    console.log(err);
})
}

const add=()=>{
    axios.post('https://localhost:7294/api/Member/AddMember',{
        Name:info.value.Name,
        Pwd:info.value.Pwd,
        Phone:info.value.Phone,
        Pic:info.value.Pic,
        Email:info.value.Email,
        State:info.value.State
    })
    .then(res=>{
        if(res.data>0){
            alert("添加成功");
            router.push("MemberShow")
        }
        else{
            alert("添加失败");
            return;
        }
    })
    .catch(err=>{
        console.log(err);
    })
}



</script>

<style scoped>

</style>